//
//  HQYSuCollectionView.m
//  HONGQIYUN
//
//  Created by leihuiwu on 16/12/22.
//  Copyright © 2016年 Haidi. All rights reserved.
//

#import "HQYSuCollectionView.h"

#import "HQYSuCollectionViewInterceptor.h"

@interface HQYSuCollectionView ()

@property (nonatomic, strong) HQYSuCollectionViewInterceptor * dataSourceInterceptor;


@end


@implementation HQYSuCollectionView


#pragma mark - LayoutSubviews Override
- (void)layoutSubviews {
  //  [self resetContentOffsetIfNeeded];
    [super layoutSubviews];
}

- (void)resetContentOffsetIfNeeded {
    CGPoint contentOffset  = self.contentOffset;
    //scroll over top
    if (contentOffset.x < 0.0) {
        contentOffset.x = self.contentSize.width / 3.0;
    }
    //scroll over bottom
    else if (contentOffset.x >= (self.contentSize.width - self.bounds.size.width)) {
        contentOffset.x = self.contentSize.width / 3.0 - self.bounds.size.width;
    }
    [self setContentOffset: contentOffset];
}

#pragma mark - DataSource Delegate Setter/Getter Override
- (void)setDataSource:(id<UICollectionViewDataSource>)dataSource {
    self.dataSourceInterceptor.receiver = dataSource;
    [super setDataSource:(id<UICollectionViewDataSource>)self.dataSourceInterceptor];
}


- (HQYSuCollectionViewInterceptor *)dataSourceInterceptor {
    if (!_dataSourceInterceptor) {
        _dataSourceInterceptor = [[HQYSuCollectionViewInterceptor alloc]init];
        _dataSourceInterceptor.middleMan = self;
    }
    return _dataSourceInterceptor;
}


#pragma mark - Delegate Method Override
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    self.actualRows = [self.dataSourceInterceptor.receiver collectionView:collectionView numberOfItemsInSection:section];
    return self.actualRows * 10000;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath * actualIndexPath = [NSIndexPath indexPathForRow:indexPath.row % self.actualRows inSection:indexPath.section];
    return [self.dataSourceInterceptor.receiver collectionView:collectionView cellForItemAtIndexPath:actualIndexPath];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
